package org.nerve.sprider.worker;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import org.nerve.sprider.common.DBUtil;
import org.nerve.sprider.common.StringUtil;
import org.nerve.sprider.domain.WebPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * org.nerve.sprider.worker
 * Created by nerve on 2016/3/22.
 */
public class ExportToFileWorker implements Worker{

	private static Logger logger= LoggerFactory.getLogger(ExportToFileWorker.class);

	private Dao<WebPage, Long> dao;
	private long limit=10;    //max
	private String outPath;
	private Path path;

	public ExportToFileWorker() throws Exception {
		dao= DBUtil.getDao(WebPage.class);

		outPath = StringUtil.getRunJarPath()+ File.separator+"output"
				+File.separator+new SimpleDateFormat("yyyyMMdd").format(new Date());

		path=Paths.get(outPath);
		if(!Files.exists(path)){
			Files.createDirectories(path);
		}

		logger.info("path="+outPath);
	}

	@Override
	public void doWork(Map<String, Object> params) throws Exception {
		long total=dao.countOf();
		long index=0;
		for(;index<=total;index+=limit){
			logger.info("fetching results from "+index+", to "+(index+limit));

			QueryBuilder<WebPage,Long> qb=dao.queryBuilder();
			qb.limit(limit);
			qb.offset(index);

			List<WebPage> pages=dao.query(qb.prepare());
			for(WebPage wp:pages)
				writeToHtml(wp);
		}
	}

	private void writeToHtml(WebPage wp) throws IOException {
		Path p=Paths.get(outPath, wp.getHtmlMd5()+".html");
		try{
			Files.write(p, wp.getHtml().getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
			logger.info("write ok to "+p.toAbsolutePath());
		}catch (Exception e){
			logger.info("error on write to "+p.toAbsolutePath());
			logger.error(e.getMessage(),e);
		}
	}
}